﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using saveeditedrecordscodefile;
using adddependentscodefile;
using addqualificationcodefile;

namespace Employee_Management_System
{
    public partial class EditForm : Form
    {
        LoginForm formLogin;
        AdminForm formAdmin;
        saveedited se = new saveedited();
        adddependents adddeps = new adddependents();
        addqualification addqual = new addqualification();
        public string num="";
        public int ind = 0;

        public EditForm(LoginForm parent, int index, string number)
        {
            formLogin = parent;
            InitializeComponent();
            num = number;
            ind = index;
            bback.Click += new EventHandler(btnback_Click);
            bsave.Click += new EventHandler(btnsave_Click);
            blogout.Click += new EventHandler(btnlogout_Click);
            badddependents.Click += new EventHandler(btnadddependents_Click);
            baddcareer.Click += new EventHandler(btnaddcareer_Click);
            this.Load += new EventHandler(EditForm_Load);
            se.savecompleted += new EventHandler(se_savecompleted);
            bshowlabel.Click += new EventHandler(btnshow_Click);
            addqual.addcompleted += new EventHandler(addqual_addcompleted);
            adddeps.addcompleted += new EventHandler(adddeps_addcompleted);
        }

        

        

        void btnshow_Click(object sender, EventArgs e)
        {
            if (txtboardexam.Text == "")
            {
                txtboardexam.Text = "Board Exams Rating";
            }
            if (txtcivilservice.Text == "")
            {
                txtcivilservice.Text = "CSC Eligibility & Rating";
            }
            if (txtdriverslicense.Text == "")
            {
                txtdriverslicense.Text = "Driver's License and Restriction";
            }
            if (txtprcnumber.Text == "")
            {
                txtprcnumber.Text = "PRC Number";
            }
            if (txttesda.Text == "")
            {
                txttesda.Text = "Tesda NC Title & Cert. no.";
            }
            if (txtothers.Text == "")
            {
                txtothers.Text = "Others";
            }

            if (txttin.Text == "")
            {
                txttin.Text = "TIN";
            }
            if (txtsss.Text == "")
            {
                txtsss.Text = "SSS";
            }
            if (txtphilhealth.Text == "")
            {
                txtphilhealth.Text = "PHILHEALTH";
            }
            if (txtpagibig.Text== "")
            {
                txtpagibig.Text = "PAGIBIG";
            }

            if (txtpresadd5.Text == "")
            {
                txtpresadd5.Text = "Tel. No.";
            }
            if (txtpermadd5.Text == "")
            {
                txtpermadd5.Text = "Tel. No.";
            }

            if (txtempcontact.Text == "")
            {
                txtempcontact.Text = "CP/LANDLINE";
            }
            if (txtgender.Text == "")
            {
                txtgender.Text = "Gender";
            }
            if (txtempheight.Text == "")
            {
                txtempheight.Text = "Height";
            }
            if (txtempweight.Text == "")
            {
                txtempweight.Text = "Weight";
            }

            if (txtmother2.Text == "")
            {
                txtmother2.Text = "Address";
            }
            if (txtmother3.Text == "")
            {
                txtmother3.Text = "Tel. No.";
            }
            if (txtfather2.Text == "")
            {
                txtfather2.Text = "Address";
            }
            if (txtfather3.Text == "")
            {
                txtfather3.Text = "Tel. No.";
            }
        }

        //saving edited records of employeee
        void se_savecompleted(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                EventHandler eh = new EventHandler(se_savecompleted);
                this.Invoke(eh);
                return;
            }
            
            se.donCommit();
            MessageBox.Show("Employee Successfully Added!");
            settext();
            setdependents();
            setcareer();
        }
        void btnsave_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Successfully Saved!");
            string name = txtsurname.Text + ", " + txtfirstname.Text + " y " + txtmiddlename.Text;
            string presadd = txtpresadd1.Text + " " + txtpresadd2.Text + " " + txtpresadd3.Text + ", " + txtpresadd4.Text;
            string permadd = txtpermadd1.Text + " " + txtpermadd2.Text + " " + txtpermadd3.Text + ", " + txtpermadd4.Text;

            string marital = "";

            if (radbtnsingle.Checked == true)
            {
                marital = radbtnsingle.Text;
            }
            if (radbtnmarried.Checked == true)
            {
                marital = radbtnmarried.Text;
            }
            if (radbtnwidow.Checked == true)
            {
                marital = radbtnwidow.Text;
            }
            if (radbtnseparated.Checked == true)
            {
                marital = radbtnseparated.Text;
            }
            string bday = combomonth.Text + " " + comboday.Text + ", " + comboyear.Text;
            string spouse = txtspouse2.Text + " " + txtspouse3.Text + " " + txtspouse1.Text;
            se.asyncsaveprofile(ind, txtempnum.Text, combostatus.Text, name, txtdept.Text, txtpos.Text, txtgender.Text, txtempcontact.Text,
                presadd, permadd, txtempheight.Text, txtempweight.Text, marital, bday, txtage.Text, txttin.Text, txtsss.Text, txtphilhealth.Text,
                txtpagibig.Text, comboeducattain.Text, txtfather1.Text, txtfather2.Text, txtfather3.Text, txtmother1.Text,
                txtmother2.Text, txtmother3.Text, spouse, txtspouse4.Text, txtboardexam.Text, txtcivilservice.Text, txtdriverslicense.Text,
                txtprcnumber.Text, txttesda.Text, txtothers.Text, txtsurname.Text);
        }
        //end of saving sdited record of employee

        void EditForm_Load(object sender, EventArgs e)
        {
            //ae.dbQuery("SELECT fID, fQualifiedFields FROM tblQualification WHERE fEmployeeNumber='" + lbresults.SelectedItem.ToString() + "'");
            se.dbConnect();
            se.dbQuery("SELECT * FROM tblProfiles WHERE fEmployeeNumber='"+num+"' ");
            /*adddeps.dbConnect();
            adddeps.dbQuery("SELECT * FROM tblDependents");
            addqual.dbConnect();
            addqual.dbQuery("SELECT * FROM tblQualification");*/
            ind = 0;
            txtexistingempname.Text = se.donResult.Rows[ind]["fEmployeeName"].ToString();
            txtempnum.Text = se.donResult.Rows[ind]["fEmployeeNumber"].ToString();
            combostatus.Text = se.donResult.Rows[ind]["fEmployeeStatus"].ToString();
            txtgender.Text = se.donResult.Rows[ind]["fGender"].ToString();
            txtempcontact.Text=se.donResult.Rows[ind]["fContact"].ToString();
            txtpresadd.Text = se.donResult.Rows[ind]["fPresentAddress"].ToString();
            txtpermadd.Text = se.donResult.Rows[ind]["fPermanentAddress"].ToString();
            txtempheight.Text = se.donResult.Rows[ind]["fHeight"].ToString();
            txtempweight.Text = se.donResult.Rows[ind]["fWeight"].ToString();
            txtage.Text = se.donResult.Rows[ind]["fAge"].ToString();
            txtexistingempbday.Text=se.donResult.Rows[ind]["fBirthdate"].ToString();
            txtfather1.Text = se.donResult.Rows[ind]["fFatherName"].ToString();
            txtfather2.Text = se.donResult.Rows[ind]["fFatherAddress"].ToString();
            txtfather3.Text = se.donResult.Rows[ind]["fFatherNumber"].ToString();
            txtmother1.Text = se.donResult.Rows[ind]["fMotherName"].ToString();
            txtmother2.Text = se.donResult.Rows[ind]["fMotherAddress"].ToString();
            txtmother3.Text = se.donResult.Rows[ind]["fMotherNumber"].ToString();
            txtspouse4.Text = se.donResult.Rows[ind]["fSpouseOccupation"].ToString();
            txtexistingspousename.Text = se.donResult.Rows[ind]["fSpouseName"].ToString();
            txtdept.Text = se.donResult.Rows[ind]["fDepartment"].ToString();
            txtpos.Text = se.donResult.Rows[ind]["fPosition"].ToString();            
            txttin.Text = se.donResult.Rows[ind]["fTIN"].ToString();
            txtsss.Text = se.donResult.Rows[ind]["fSSS"].ToString();
            txtphilhealth.Text = se.donResult.Rows[ind]["fPHILHEALTH"].ToString();
            txtpagibig.Text = se.donResult.Rows[ind]["fPagibig"].ToString();       
            comboeducattain.Text = se.donResult.Rows[ind]["fEducAttain"].ToString();


            txtboardexam.Text = se.donResult.Rows[ind]["fBoardExamRating"].ToString();
            txtcivilservice.Text = se.donResult.Rows[ind]["fCSCEligibility"].ToString();
            txtdriverslicense.Text = se.donResult.Rows[ind]["fDriversLicense"].ToString();
            txtprcnumber.Text = se.donResult.Rows[ind]["fPRCNumber"].ToString();
            txttesda.Text = se.donResult.Rows[ind]["fTesdaNCTitle"].ToString();
            txtothers.Text = se.donResult.Rows[ind]["fOthers"].ToString();
            
        }

        //Adding Career
        void addqual_addcompleted(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                EventHandler eh = new EventHandler(addqual_addcompleted);
                this.Invoke(eh);
                return;
            }
            addqual.donCommit();
        }
        void btnaddcareer_Click(object sender, EventArgs e)
        {
            if (checkcareer1.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer1.Text);
            }
            if (checkcareer2.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer2.Text);
            }
            if (checkcareer3.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer3.Text);
            }
            if (checkcareer4.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer4.Text);
            }
            if (checkcareer5.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer5.Text);
            }
            if (checkcareer6.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer6.Text);
            }
            if (checkcareer7.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer7.Text);
            }
            if (checkcareer8.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer8.Text);
            }
            if (checkcareer9.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer9.Text);
            }
            if (checkcareer10.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer10.Text);
            }
            if (checkcareer11.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer11.Text);
            }
            if (checkcareer12.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer12.Text);
            }
            if (checkcareer13.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer13.Text);
            }
            if (checkcareer14.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer14.Text);
            }
            if (checkcareer15.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer15.Text);
            }
            if (checkcareer16.Checked == true)
            {
                addqual.asyncaddqualification(txtempnum.Text, checkcareer16.Text);
            }
            if (txtcareer1.Text != "" && txtcareer1.Text != "Others")
            {
                addqual.asyncaddqualification(txtempnum.Text, txtcareer1.Text);
            }
            if (txtcareer2.Text != "" && txtcareer2.Text != "Others")
            {
                addqual.asyncaddqualification(txtempnum.Text, txtcareer2.Text);
            }
            MessageBox.Show("Qualifications Successfully Added!");
            setcareer();
        }
        //end of Adding Career


        //Adding dependents
        void adddeps_addcompleted(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                EventHandler eh = new EventHandler(adddeps_addcompleted);
                this.Invoke(eh);
                return;
            }
            adddeps.donCommit();

            MessageBox.Show("Dependents Successfully Added!");
            setdependents();
        }
        void btnadddependents_Click(object sender, EventArgs e)
        {
            string depname = txtchild1b.Text + " " + txtchild1c.Text + ". " + txtchild1a.Text;
            adddeps.asyncadddependents(txtempnum.Text, depname, txtchild1d.Text, txtchild1e.Text, txtchild1f.Text); 
        }
        //end of adding dependents

        void btnlogout_Click(object sender, EventArgs e)
        {
            formLogin = new LoginForm();
            this.Hide();
            formLogin.Show();
            settext();

        }



        void btnback_Click(object sender, EventArgs e)
        {
            formAdmin = new AdminForm(formLogin);
            this.Hide();
            formAdmin.Show();
            settext();
            setdependents();
            setcareer();
            ind = 0;
            num = "";
        }
        void settext()
        {
            txtempnum.Text = "";
            combostatus.Text = "Status";
            txtsurname.Text = "Surname";
            txtfirstname.Text = "First Name";
            txtmiddlename.Text = "Middle Name";
            txtgender.Text = "Gender";
            txtempcontact.Text = "CP/LANDLINE";
            txtpresadd1.Text = "No.";
            txtpresadd2.Text = "Street";
            txtpresadd3.Text = "City";
            txtpresadd4.Text = "Province";
            txtpresadd5.Text = "Tel. No.";
            txtpermadd1.Text = "No.";
            txtpermadd2.Text = "Street";
            txtpermadd3.Text = "City";
            txtpermadd4.Text = "Province";
            txtpermadd5.Text = "Tel. No.";
            txtempheight.Text = "Height";
            txtempweight.Text = "Weight";
            radbtnsingle.Checked = false;
            radbtnmarried.Checked = false;
            radbtnseparated.Checked = false;
            radbtnwidow.Checked = false;
            combomonth.Text = "Month";
            comboyear.Text = "Year";
            comboday.Text = "Day";
            txtage.Text = "Age";
            txtfather1.Text = "Father's Name";
            txtfather2.Text = "Address";
            txtfather3.Text = "Tel. No.";
            txtmother1.Text = "Mother's Name";
            txtmother2.Text = "Address";
            txtmother3.Text = "Tel. No.";
            txtspouse1.Text = "LastName";
            txtspouse2.Text = "FirstName";
            txtspouse3.Text = "MiddleName";
            txtspouse4.Text = "Occupation";
            
            comboeducattain.Text = "Highest Educational Attainment";
            txtboardexam.Text = "Board Exams Rating";
            txtcivilservice.Text = "CSC Eligibility & Rating";
            txtdriverslicense.Text = "Driver's License and Restriction";
            txtprcnumber.Text = "PRC Number";
            txttesda.Text = "Tesda NC Title & Cert. No.";
            txtothers.Text = "Others";
            txtdept.Text = "Department Assigned";
            txtpos.Text = "Position";
            txttin.Text = "TIN";
            txtsss.Text = "SSS";
            txtphilhealth.Text = "PHILHEALTH";
            txtpagibig.Text = "PAGIBIG";
            
        }
        void setdependents()
        {
            txtchild1a.Text = "LastName";
            txtchild1b.Text = "FirstName";
            txtchild1c.Text = "MiddleName";
            txtchild1d.Text = "Age";
            txtchild1e.Text = "Sex";
            txtchild1f.Text = "Birthdate";
        }
        void setcareer()
        {
            checkcareer1.Checked = false;
            checkcareer2.Checked = false;
            checkcareer3.Checked = false;
            checkcareer4.Checked = false;
            checkcareer5.Checked = false;
            checkcareer6.Checked = false;
            checkcareer7.Checked = false;
            checkcareer8.Checked = false;
            checkcareer9.Checked = false;
            checkcareer10.Checked = false;
            checkcareer11.Checked = false;
            checkcareer12.Checked = false;
            checkcareer13.Checked = false;
            checkcareer14.Checked = false;
            checkcareer15.Checked = false;
            checkcareer16.Checked = false;
            txtcareer1.Text = "Others";
            txtcareer2.Text = "Others";
        }
    }
}
